<?php
namespace App\Http\Controllers;

class IdcardImageUpload
{
    public  function checkApi()
    {
        // 定义公共参数
        $data = [
            'appId' => 'openapitest',
            'identity'=>'141024197503201571',
            'identityType'=>0,
            'nonce'=>$this->createNoncestr(),
            'name'=>'姓名',
        ];

        // 获取需要上传的正反面图片,用绝对路径
        $frontfile = 'fm1.jpeg';
        $backfile = 'fm6.jpeg';

        /* 开始签名 */
        $sign_data = $data;                                     // 定义签名参数
        $bodys['frontfile'] = file_get_contents($frontfile);    // 获取身份证正面图片的byte []
        $bodys['backfile'] = file_get_contents($backfile);      // 获取身份证反面图片的byte []
        $sign_data['frontfile'] = md5($bodys['frontfile']);     // 设置验签参数frontfile的值为md5(byte [])
        $sign_data['backfile'] = md5($bodys['backfile']);       // 设置验签参数backfile的值为md5(byte [])
        $sign = $this->rsaSign($sign_data);                     // 签名并获取签名参数sign的值
        /* 完成签名 */

        $post_data = $data; // 定义post数据
        $minetype = 'image/jpeg';
        $post_data['frontfile'] = curl_file_create( realpath($frontfile), $minetype);   // 设置post数据中的frontfile参数
        $post_data['backfile'] = curl_file_create( realpath($backfile), $minetype);     // 设置post数据中的backfile参数
        $post_data['sign'] = $sign;                                                     // 设置post数据中的sign参数
        $url = 'https://openapitest.aiyuangong.com/econtract/extr/identity/upload';     // post请求url
        $header = ['content-type:multipart/form-data'];                                 // post请求ContentType
        $res_data = $this->post($url, $post_data, $header);                             // 发起请求

    }

    public function rsaSign($data) {

        ksort($data);

        $query = http_build_query($data);
        $query = urldecode($query);

        $private_key = '私钥';
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($private_key, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";

        openssl_sign($query, $sign, $res,OPENSSL_ALGO_SHA256);
        //base64编码
        $sign = base64_encode($sign);
        return $sign;
    }

    protected function post($path, $params = [], $headers)
    {

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $path);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

        //CURL_SSLVERSION_TLSv1
        curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            $errmsg = curl_error($curl);
            curl_close($curl);

            return compact('errorno', 'errmsg');
        }
        curl_close($curl);

        return json_decode($res, true);
    }

    /**
     * 	作用：产生随机字符串，不长于32位
     */
    public function createNoncestr( $length = 32 )
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
}